package com.taptech.doufu.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.constant.IntentKey;
import com.taptech.doufu.bean.AlbumsBean;
import com.taptech.doufu.bean.cartoon.CartoonBean;
import com.taptech.doufu.bean.PersonalNoteBean;
import com.taptech.doufu.constant.FragmentLoadType;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.ui.view.drawcircle.DrawCircleService;
import com.taptech.doufu.ui.fragment.DoufuContentFragment;
import com.taptech.doufu.ui.fragment.NovelChildFragments.RankNovelFragment;
import com.taptech.doufu.ui.fragment.NovelWelfareFragment;
import com.taptech.doufu.services.CartoonServices;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

/**
 * Created by longsiyang on 16/4/14.
 */
public class ContainerFragmentActivity extends DiaobaoBaseActivity
{
    ImageView ivBack,ivHelp;
    TextView tv_titleName;
    RelativeLayout rl_container;
    String url="";
    int coverType;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_container_fragment);
        setViews();
        setDataWithIntent();
    }

    private void setViews()
    {
        rl_container=(RelativeLayout)findViewById(R.id.rl_container);
        ivBack = (ImageView) findViewById(R.id.iv_container_back);
        ivHelp=(ImageView)findViewById(R.id.iv_container_help);
        tv_titleName = (TextView) findViewById(R.id.tv_container_titlename);
    }

    private void setDataWithIntent()
    {
        ivBack.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                ContainerFragmentActivity.this.finish();
            }
        });
        String titleName = getIntent().getStringExtra(FragmentLoadType.fragmentTypeName);
        url=getIntent().getStringExtra(Constant.URL);
        coverType = getIntent().getIntExtra(IntentKey.INTENT_KEY_COVER_TYPE, 0);
        if ((titleName != null) && (titleName.length() > 0))
        {
            tv_titleName.setText(titleName);
        } else
        {
            rl_container.setVisibility(View.GONE);
        }

        int fragmentType = getIntent().getIntExtra(FragmentLoadType.fragmentType, 0);
        //如果是小说排行，择重新设置样式
        if(FragmentLoadType.NOVELRANK==fragmentType){
            ivHelp.setVisibility(View.VISIBLE);
            ivHelp.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Intent intent = new Intent(ContainerFragmentActivity.this, BrowseActivity.class);
                    intent.putExtra(Constant.URL, "http://api.doufu.la/index.php/journal/detail?id=49");
                    ContainerFragmentActivity.this.startActivity(intent);
                }
            });
        }else if(FragmentLoadType.CARTOONRANK==fragmentType){
            //漫画
            ivHelp.setVisibility(View.VISIBLE);
            ivHelp.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Intent intent = new Intent(ContainerFragmentActivity.this, BrowseActivity.class);
                    intent.putExtra(Constant.URL, "http://api.doufu.la/index.php/journal/detail?id=70");
                    ContainerFragmentActivity.this.startActivity(intent);
                }
            });
        }

        if (getLoadFragment(fragmentType) != null)
        {
            FragmentTransaction mFragment = getSupportFragmentManager().beginTransaction();
            mFragment.add(R.id.frame_container, getLoadFragment(fragmentType)).commit();
        }
    }

    private Fragment getLoadFragment(int type)
    {
        switch (type)
        {
            case FragmentLoadType.NOVELRANK:
                return new RankNovelFragment(url);
            case FragmentLoadType.NOVEWELFARE:
                return new NovelWelfareFragment(url);
            case FragmentLoadType.CARTOONORIGINAL:
                CartoonLatestFragment cartoonOrighinalFragment = new CartoonLatestFragment();
                cartoonOrighinalFragment.setArgument("drawings", PersonalNoteBean.class, DrawCircleService.HANDLE_TYPE_LOAD_DRAW_CARTOON
                        , DrawCircleService.BASE_URL + "journals/drawings?" +  "&type=" + "2" + "&size=" + Constant.PAGE_SIZE_10);
                return cartoonOrighinalFragment;
            case FragmentLoadType.CARTOONRANK://排行
                DoufuContentFragment cartoonRankFragment = new DoufuContentFragment();
                cartoonRankFragment.setArgument("items", AlbumsBean.class, DrawCircleService.HANDLE_TYPE_LOAD_DRAW_CARTOON
                        , DrawCircleService.BASE_URL + "albums/items?id=355" +  "&size=" + Constant.PAGE_SIZE_10);
                return cartoonRankFragment;
            case FragmentLoadType.CARTOONFINISH:
                DoufuContentFragment cartoonLibraryFragment = new DoufuContentFragment(true);
                cartoonLibraryFragment.setArgument("list", CartoonBean.class, CartoonServices.HANDLE_TYPE_LOAD_CARTOON
                        ,url+"?");
                return cartoonLibraryFragment;
            case FragmentLoadType.CARTOONSORTDES:
                DoufuContentFragment cartoonSortDesFragment = new DoufuContentFragment(true);
                if(url.contains("?")){
                    cartoonSortDesFragment.setArgument("list", CartoonBean.class, CartoonServices.HANDLE_TYPE_LOAD_CARTOON
                            ,url);
                }else{
                    cartoonSortDesFragment.setArgument("list", CartoonBean.class, CartoonServices.HANDLE_TYPE_LOAD_CARTOON
                            ,url+"?");
                }
                return cartoonSortDesFragment;
            case FragmentLoadType.RECOMMENDMORE:
                DoufuContentFragment cartoonRecommendFragment = new DoufuContentFragment(true);
                if(url.contains("?")){
                    cartoonRecommendFragment.setArgument("comics", CartoonBean.class, CartoonServices.HANDLE_TYPE_LOAD_CARTOON
                            ,url,coverType);
                }else{
                    cartoonRecommendFragment.setArgument("comics", CartoonBean.class, CartoonServices.HANDLE_TYPE_LOAD_CARTOON
                            ,url+"?",coverType);
                }
                return cartoonRecommendFragment;
            case FragmentLoadType.CARTOONWELFARE:
                DoufuContentFragment cartoonWelfareFragment = new DoufuContentFragment();
                cartoonWelfareFragment.setArgument("items", AlbumsBean.class, DrawCircleService.HANDLE_TYPE_LOAD_DRAW_CARTOON
                        , url);
                return cartoonWelfareFragment;
            default:
                break;
        }
        return null;
    }
}
